package com.zysd.collection.scheduler.service.record;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Iterator;
import java.util.Map;

/**
 * 档案接口抽象类
 *
 * @author liuht
 * @date 2017/11/13 10:25
 */
public abstract class AbstractRecordApiService {
    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractRecordApiService.class);

    protected static final String RESTE_RROR = "RestService error: ";

    /**
     * 售电采集档案接口 url
     */
    @Value("${zysd.record.server-url}")
    protected String recordUrl;

    @Autowired
    protected RestTemplate restTemplate;

    /**
     * get 拼接参数
     *
     * @param url    请求地址
     * @param params 参数map
     * @return String 结果url
     */
    protected String setGetParam(String url, Map<String, Object> params) {
        if (!CollectionUtils.isEmpty(params)) {
            // 遍历参数
            Iterator<String> it = params.keySet().iterator();
            StringBuilder urlBuilder = new StringBuilder(url);
            while (it.hasNext()) {
                String key = it.next();
                if (!StringUtils.isEmpty(params.get(key))) {
                    if (urlBuilder.toString().contains("?")) {
                        urlBuilder.append("&");
                    } else {
                        urlBuilder.append("?");
                    }
                    urlBuilder.append(key).append("=").append(params.get(key));
                }
            }
            url = urlBuilder.toString();
        }
        return url;
    }
}

